import requests
import json


def get_maoyan_comments(movie_id, offset=0, limit=15):
    url = f"http://m.maoyan.com/mmdb/comments/movie/{movie_id}.json"
    params = {
        "offset": offset,
        "limit": limit
    }
    headers = {
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"
    }

    try:
        response = requests.get(url, params=params, headers=headers)
        if response.status_code == 200:
            return response.json()
        else:
            print(f"请求失败，状态码：{response.status_code}")
            return None
    except Exception as e:
        print(f"发生错误：{e}")
        return None


# 示例：获取电影《侏罗纪世界：重生》(ID:1520447)的前15条评论
movie_id = "1520447"
comments_data = get_maoyan_comments(movie_id)

if comments_data:
    for comment in comments_data.get("cmts", []):
        print(f"用户: {comment['nickName']}")
        print(f"评分: {comment['score']}")
        print(f"评论: {comment['content']}")
        print(f"时间: {comment['startTime']}")
        print("=" * 50)